package com.wangshuqi.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RedirectServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("重定向到新的地址");
        String redirectUrl = req.getParameter("redirectUrl");
        // 重定向方式一：
        //    1.设置响应状态为302 ，浏览器认为状态为302的响应为重定向
        //    2.设置响应头 key：Location value：重定向url，任何url都可以，外部url也可以
        /*resp.setStatus(302);
        resp.setHeader("Location", redirectUrl);*/

        // 重定向方式二：
        resp.sendRedirect(redirectUrl);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
